using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Reflection;


namespace THSLib
{
    public class BaseBusinessController
    {
        protected DALBaseProvider dal;        

        #region Constructor
        public BaseBusinessController()
        {
            dal = new DALBaseProvider();
        }

        public BaseBusinessController(String strTableName, Type objType)
        {
            dal = new DALBaseProvider(strTableName, objType);
        }

        public BaseBusinessController(Type objType)
        {
            string strTableName = objType.Name.Substring(0, objType.Name.Length - 4);
            dal = new DALBaseProvider(strTableName, objType);
        }
        #endregion

        #region Utility Functions
        public int GetMaxID()
        {
            return dal.GetMaxID() + 1;
        }
       
        #endregion

        #region Functions for Create,Update,Delete
        public virtual int CreateObject(BusinessObject obj)
        {            
            dal.SetValueToPrimaryColumn(obj, GetMaxID());  
            return dal.CreateObject(obj);
        }

        public virtual int CreateObject(BusinessObject obj,int iObjectID)
        {
            if (IsExist(iObjectID))
                return -1;

            dal.SetValueToPrimaryColumn(obj, iObjectID);
            return dal.CreateObject(obj);
        }

        public virtual int CreateObject(BusinessObject obj,DbTransaction transaction)
        {
            dal.SetValueToPrimaryColumn(obj, GetMaxID());            
            return dal.CreateObject(obj, transaction);
        }

        public virtual int UpdateObject(BusinessObject obj)
        {
            return dal.UpdateObject(obj);
        
        }

        public virtual int UpdateObject(BusinessObject obj, DbTransaction transaction)
        {
            return dal.UpdateObject(obj, transaction);
        }

        public virtual void DeleteObject(int iObjectID)
        {
            dal.DeleteObject(iObjectID);
        }

        public virtual void DeleteObject(int iObjectID, DbTransaction transaction)
        {
            dal.DeleteObject(iObjectID, transaction);
        }

        public void DeleteAllObjects()
        {
            dal.DeleteAllObjects();
        }

        public virtual void DeleteByForeignColumn(String strForeignColumn, object objForeignColumnValue)
        {
            dal.DeleteByForeignColumn(strForeignColumn, objForeignColumnValue);
        }

        
        #endregion

        #region Functions for Get Object

        public virtual object GetTemplateObject()
        {
            return dal.GetTemplateObject();
        }
        public virtual object GetObjectByID(int iObjectID)
        {
            return dal.GetObjectById(iObjectID);
        }

        public virtual DataSet GetDataSetByID(int iObjectID)
        {
            return dal.GetDataSetByID(iObjectID);
        }

        public virtual object GetDeletedObjectByID(int iObjectID)
        {
            return dal.GetDeletedObjectByID(iObjectID);
        }

        public virtual String GetObjectNameByID(int iObjectID)
        {
            return dal.GetObjectNameByID(iObjectID);
        }

        public virtual String GetObjectNoByID(int iObjectID)
        {
            return dal.GetObjectNoByID(iObjectID);
        }
       
        public virtual object GetObjectByName(String strObjectName)
        {
            return dal.GetObjectByName(strObjectName);
        }

        public virtual object GetObjectByNo(String strObjectNo)
        {
            return dal.GetObjectByNo(strObjectNo);
        }

        public virtual object GetObjectByAANumberInt(int iAANumberInt)
        {
            return dal.GetObjectByAANumberInt(iAANumberInt);
        }

        public virtual int GetObjectIDByName(String strObjectName)
        {
            return dal.GetObjectIDByName(strObjectName);
        }

        public virtual int GetObjectIDByNo(String strObjectNo)
        {
            return dal.GetObjectIDByNo(strObjectNo);
        }

        public virtual DataSet GetAllObjects()
        {
            return dal.GetAllObject();
        }

        public virtual DataSet GetAllProfileObjects()
        {
            return dal.GetAllProfileObjects();
        }

        public virtual DataSet GetAllAliveObjects()
        {
            return dal.GetAllAliveObjects();
        }

        public virtual DataSet GetObjectsForDataLookup(String[] arrColumns, int iMaxResults)
        {
            String strQuery = String.Format("Select TOP({0}) ",iMaxResults);
            foreach (String strColumn in arrColumns)
            {
                strQuery += String.Format("[{0}],", strColumn);
            }
            strQuery=strQuery.Remove(strQuery.Length - 1, 1);
            strQuery += String.Format(" FROM [{0}] WHERE [AAStatus]='Alive'",dal.TableName);
            DbCommand cmd = SqlDbHelper.GetQuery(strQuery);
            return (DataSet)SqlDbHelper.RunQuery(cmd);
        }

        public virtual DataSet GetObjectsByIDForDataLookup(String[] arrColumns, int iObjectID)
        {
            String strQuery = "Select ";
            String strPrimaryColumn = THSDbUtil.GetTablePrimaryColumn(dal.TableName);

            foreach (String strColumn in arrColumns)
            {
                strQuery += String.Format("[{0}],", strColumn);
            }
            strQuery = strQuery.Remove(strQuery.Length - 1, 1);
            strQuery += String.Format(" FROM [{0}] WHERE [{1}]={2} AND [AAStatus]='Alive'", dal.TableName,strPrimaryColumn,iObjectID);
            DbCommand cmd = SqlDbHelper.GetQuery(strQuery);
            return (DataSet)SqlDbHelper.RunQuery(cmd);
        }

        public virtual DataSet GetObjectsForDataLookup(String[] arrColumns, int iMaxResults, String strCondition)
        {
            String strQuery = String.Format("Select TOP({0}) ", iMaxResults);
            foreach (String strColumn in arrColumns)
            {
                strQuery += String.Format("[{0}],", strColumn);
            }
            strQuery = strQuery.Remove(strQuery.Length - 1, 1);
            strQuery += String.Format(" FROM [{0}] WHERE [AAStatus]='Alive'", dal.TableName);
            strQuery += " AND " + strCondition;

            DbCommand cmd = SqlDbHelper.GetQuery(strQuery);
            return (DataSet)SqlDbHelper.RunQuery(cmd);
        }

        //hieu nguyen created 21,2009
        //get object by updated date
        public virtual DataSet GetObjectsByAAUpdatedDate(String strTableName, DateTime dateValue)
        {
            return (DataSet)dal.GetObjectsByUpdatedDate(strTableName, dateValue);
        }
        public virtual DataSet GetDataSet(String strQuery)
        {
            return (DataSet)dal.GetDataSet(strQuery);
        }


        public virtual object GetFirstObject()
        {
            return dal.GetFirstObject();
        }

        public virtual int GetFirstObjectID()
        {
            try
            {
                object obj = GetFirstObject();
                if (obj != null)
                    return Convert.ToInt32(dal.GetPrimaryColumnValue(obj));
                return 0;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public virtual object GetObjectFromDataRow(DataRow row)
        {
            return dal.GetObjectFromDataRow(row);
        }

        public DataRow GetDataRowFromBusinessObject(DataRow row, BusinessObject obj)
        {
            foreach (DataColumn column in row.Table.Columns)
            {
                PropertyInfo property = obj.GetType().GetProperty(column.ColumnName);
                if (property != null)
                {
                    row[column.ColumnName] = property.GetValue(obj, null);
                }
            }

            return row;
        }
        public DataTable GetDataTableStructByBusinessObject(BusinessObject obj)
        {
            DataTable dtable = new DataTable();
            PropertyInfo[] cols = obj.GetType().GetProperties();
            foreach (PropertyInfo pro in cols)
            {
                dtable.Columns.Add(pro.Name, pro.PropertyType);
            }
            return dtable;
        }
        public virtual List<BusinessObject> GetListBusinessObjects()
        {
            List<BusinessObject> businessObjectList = new List<BusinessObject>();
            DataSet ds = GetAllObjects();
            if (ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    BusinessObject objBusinessObject = (BusinessObject)SqlDbHelper.GetObjectFromDataRow(row, dal.ObjectType);
                    businessObjectList.Add(objBusinessObject);
                }
            }
            return businessObjectList;
        }

        public virtual DataSet GetAllDataByForeignColumn(String strForeignColumnName, object objForeignColumnValue)
        {
            return (DataSet)dal.GetAllDataByForeignColumn(strForeignColumnName, objForeignColumnValue);
        }

        public virtual BusinessObject GetFirstObjectByForeignColumn(String strForeignColumnName, object objForeignColumnValue)
        {
            DataSet ds = GetAllDataByForeignColumn(strForeignColumnName, objForeignColumnValue);
            return (BusinessObject)dal.GetSingleObject(ds.Tables[0]);
        }

        public DataSet GetAllObjectsByObjectParentID(int iObjectParentID)
        {
            return (DataSet)dal.GetAllObjectsByObjectParentID(iObjectParentID);
        }

        public void DeleteAllObjectsByObjectParentID(int iObjectParentID)
        {
            dal.DeleteAllObjectsByObjectParentID(iObjectParentID);
        }

        public bool IsExist(int iObjectID)
        {
            return (GetObjectByID(iObjectID) != null);
        }

        public bool IsExistObjectName(String strObjectName)
        {
            return (GetObjectByName(strObjectName) != null);
        }

        public bool IsExist(String strObjectNo)
        {
            return (GetObjectByNo(strObjectNo) != null);
        }
        

        public bool IsExistAANumberInt(int iAANumberInt)
        {
            return (GetObjectByAANumberInt(iAANumberInt) != null);
        }

        

        #endregion

        #region Functions For Get,Delete From Owner
        public virtual DataSet GetAllFromOwner(String strOwnerTable, int iOwnerID, String strSwitcherTable)
        {
            return dal.GetMembersFromOwner(strOwnerTable, iOwnerID, dal.TableName, strSwitcherTable);
        }

        public virtual void DeleteFromOwner(String strOwnerTable, int iOwnerID, String strSwitcherTable)
        {
            dal.DeleteMembersFromOwner(strOwnerTable, iOwnerID, dal.TableName, strSwitcherTable);
        }
        #endregion

        #region Functions for main module:Search,Get New Object In Session,Update Save Status In Session

        public virtual DataSet GetAllNewObjectInSession(String strUserName)
        {
            return dal.GetNewObjectInSession(strUserName);
        }

        public virtual void UpdateObjectSaveStatusInSession(String strUserName)
        {
            dal.UpdateObjectSaveStatusInSession(strUserName);
        }

        public virtual DataSet SearchObject(object objSearch)
        {
            return (DataSet)dal.SearchObject(objSearch);
        }

        public virtual int GetRecordsCount()
        {
            int count = -1;
            String strIDFieldName = dal.TableName.Substring(0, dal.TableName.Length - 1) + "ID";

            String strSQL = String.Format("SELECT COUNT(" + strIDFieldName + ") AS Count From " + dal.TableName + " Where [AAStat] <> 'Delete' ");
            DbCommand cmd = SqlDbHelper.GetQuery(strSQL);
            DataSet ds = SqlDbHelper.RunQuery(cmd);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {

                    return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                }


            }


            return count;
        }


        #endregion                       
    }
}
